<template>
  <!-- 绘制弹框 -->
  <el-dialog :title="title" :visible.sync="dialogShow">
    <!-- 表单 -->
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px" @close="closeDialog">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" style="width:90%" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="dialogShow = false">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>

</template>

<script>
import { addPermissionApi, getPermissionInfoApi, editPermissionApi } from '@/api/permission'
export default {
  name: 'AddPerm',
  data() {
    return {
      dialogShow: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  methods: {
    // 回显
    async getRowInfo(id) {
      this.dialogShow = true
      const res = await getPermissionInfoApi(id)
      this.formData = res
    },
    async btnOK() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return console.log(error)
      }
      if (this.formData.id) {
        await editPermissionApi(this.formData)
        this.$message.success('修改成功')
      } else {
        await addPermissionApi(this.formData)
        this.$message.success('新增成功')
      }

      this.$emit('updateList')
      this.dialogShow = false
    },
    closeDialog() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style>

</style>
